#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
struct edge
{
    int to;
    int w;
    int next;
}edges[2*N];
int head[N];
int count_edge,n,m;
bool st[N];
int dis[N];
void add(int u,int v,int w)
{
    edges[count_edge].to=v;
    edges[count_edge].w=w;
    edges[count_edge].next=head[u];
    head[u]=count_edge++;
}
int bfs(int u)
{
    st[u]=true;
    queue<int> q;
    q.push(u);
    dis[u]=0;
    while(q.size())
    {
        int tem=q.front();
        q.pop();
        for(int i=head[tem];i!=-1;i=edges[i].next)
        {
            int nextnode=edges[i].to;
            if(!st[nextnode])
            {
                dis[nextnode]=dis[tem]+1;
                st[nextnode]=true;
                q.push(nextnode);
            }
        }
    }
    return dis[n];
}
int main()
{
    memset(head,-1,sizeof(head));
    memset(dis,-1,sizeof(dis));
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int u,v;
        cin>>u>>v;
        add(u,v,1);
    }
    cout<<bfs(1)<<endl;
    return 0;
}